App({
  onLaunch(options) {

    //需要判断一下storage里头有没有东西
    //第一次打开
    let $this = this;
    let globalId;
    //先处理好当天的日期
    let dateInfo = this.TodayInfo(this.globalData.startSchoolDate, new Date());
    this.globalData.weekInfo = [dateInfo.week - 1, dateInfo.day]
    console.info(this.globalData.weekInfo)

    // 删除缓存是数据
    dd.removeStorage({
      key: 'user',
      success: function(){
        console.log('删除缓存成功')
      }
    });
    //获取免登授权码
    dd.getAuthCode({
      success: function(res) {
        //然后去后台登录
        dd.httpRequest({
          url: "http://106.15.200.166:8008/user/getUserId",
          // url: "http://localhost:8008/user/getUserId",
          method: "GET",
          data: {
            code: res.authCode
          },
          success: function(res) {
            let user = res.data.data;
            // console.log(res.data.data);
            $this.globalData.id = user.ddId;
            //拿到了个人数据，存放一下个人数据
            // console.log(user.usergroup);
            dd.setStorage({
              key: 'user',
              data: {
                college: user.college,
                ddId: user.ddId,
                department: user.department,
                email: user.email,
                learnPlace: user.learnPlace,
                name: user.name,
                seat:user.seat,
                sex: user.sex,
                telephone: user.telephone,
                type: user.type,
                uNumber: user.uNumber,
                userClass: user.userclass,
                userGroup: user.usergroup
              },
              success: function() {
                // dd.alert({content: '写入成功'});
              },
              fail: function() {
                dd.showToast({
                  type: "exception",
                  content: "缓存异常,请稍后重试",
                  duration: 2000
                });
              }
            });
          },
          fail: function(err) {
            dd.showToast({
              type: "exception",
              content: "授权登录失败,请检查网络",
              duration: 2000
            });
          }
        })
      },
      fail: function(err) {
        //未能授权登录
        dd.showToast({
          type: "exception",
          content: "用户授权失败,请检查网络",
          duration: 2000
        });
      }
    })
  },
  globalData: {
    startSchoolDate: "2020-2-17",
    searchValue: "",
    id: "1",
    weekInfo: [],
    screenInfo: [],
    url:"http://106.15.200.166:8008",
    // url:"http://localhost:8008",
    judge:false
  },
  //摘抄来的
  /** 根据开学时间获取当前周数
   * 说明：
   * 一周的起始计算方式不同国家有所不同，很多其他国家将周日作为一周的开始
   * 本代码使用中国习惯，将周一作为每周的开始
   * 特此说明
   */
  TodayInfo(start, today) {
    var WEEKLEN = 7, // 一周7天为常量
      weekInfo = { "week": null, "day": null }, // 初始化返回信息，默认第null周，星期null
      oneDay = 24 * 60 * 60 * 1000, // 一天的毫秒时长
      weekLeave, // 开学当天所在周剩余天数
      weekStart, // 开学当天start是星期几
      dateDiff, // 今天与开学当天日期差
      sDate; //开学之日，日期对象
    var rDateStr = /\d{4}[\/-]\d{1,2}[\/-]\d{1,2}/g; // 简单的日期格式校验：2013/12/19
    if (!rDateStr.test(start)) {
      alert("请使用合法的开学日期！！！");
      return weekInfo;
    }
    sDate = new Date(start.replace("-", "/"));
    weekStart = sDate.getDay();
    weekStart = weekStart === 0 ? 7 : weekStart; // JS中周日的索引为0，这里转换为7，方便计算

    weekLeave = WEEKLEN - weekStart;
    weekInfo.day = today.getDay();
    today = new Date(today.getFullYear() + "/" + (today.getMonth() + 1) + "/" + today.getDate());
    dateDiff = today - sDate;
    if (dateDiff < 0) {
      alert("别开玩笑了，你还没开学呢！！！");
      return weekInfo;
    }
    dateDiff = parseInt(dateDiff / oneDay);
    weekInfo.week = Math.ceil((dateDiff - weekLeave) / WEEKLEN) + 1;
    return weekInfo;
  },
});
